from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
from datetime import datetime
import time

class ControlPurchaseStock:
    # 制定购买计划
    def __init__(self):
        self.scheduler = BackgroundScheduler()

    def make_plan(self):
        # 历每个策略类，调用每个策略类的制定购买计划函数
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "make plan")

    # 购买股票
    def purchase_stock(self):
        # 遍历每个策略类，调用每个策略类的购买股票函数
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "purchase stock")

    # 卖出股票
    def sale_stock(self):
        # 遍历每个策略类，调用每个策略类的卖出股票函数
        print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "sale stock")

    def control_test(self):
        self.scheduler.remove_job('make_plan')
        self.scheduler.remove_job('purchase_stock')
        self.scheduler.remove_job('sale_stock')
        self.scheduler.add_job(self.make_plan, 'cron', day='*', id='make_plan',  hour=9, minute=0, second=0)
        self.scheduler.add_job(self.purchase_stock, 'cron', day='*', id='purchase_stock', hour=9, minute=30, second=0)
        self.scheduler.add_job(self.sale_stock, 'cron', day='*', id='sale_stock', hour=14, minute=50, second=0)

    # 控制股票交易的函数
    def control_transaction(self):
        self.scheduler.add_job(self.control_test, 'interval', seconds=10)
        self.scheduler.add_job(self.make_plan, 'cron', day='*', hour=9, minute=0, id='make_plan')
        self.scheduler.add_job(self.purchase_stock, 'cron', day='*', hour=9, minute=35, id='purchase_stock')
        self.scheduler.add_job(self.sale_stock, 'cron', day='*', hour=14, minute=50, id='sale_stock')
        self.scheduler.start()
        while True:
            print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            time.sleep(10)

c = ControlPurchaseStock()
c.control_transaction()
print('123')


